#include "EngineerUi.h"
#include "ui_EngineerUi.h"
#include "WidgetHandler.h"
#include "Fpga.h"
#include "MiscData.h"
#include "Setting.h"
#include "SensorInfo.h"

EngineerUi::EngineerUi(QWidget *parent) :
    BaseWidget(parent),
    ui(new Ui::EngineerUi)
{
    ui->setupUi(this);
    g_functionBtn.addButton(ui->cmdLine, 0);
    g_functionBtn.addButton(ui->screenCalibration, 1);
    g_functionBtn.addButton(ui->onoff, 2);
    g_functionBtn.addButton(ui->sensor, 3);


    connect(&g_functionBtn, SIGNAL(buttonClicked(int)), SLOT(functionBtnSlot(int)));
    QObject::connect(Mcu::getInstance(), &Mcu::reportMcuVersionSignal,
                     this, &EngineerUi::reportMcuVersionSlot);
    m_backLabel = new QLabel("",this);
    m_backLabel->move(0,0);
    m_backLabel->resize(1280,800);
    m_backLabel->hide();
    system("ifconfig eth0 down");
    system("ifconfig eth1 up");
    system("ifconfig eth1 192.168.1.31");
}

EngineerUi::~EngineerUi()
{
    delete ui;
}

void EngineerUi::activate()
{
    ui->pushButton->hide();
    ui->lineEdit_2->hide();
    ui->onoff->hide();
    ui->sensor->hide();
    ui->page->setCurrentIndex(0);
    Mcu::getInstance()->readMainframeMcuVersionMessage();
    ui->armVerson->setText(QString(tr(" arm:  %1")).arg(ARM_VERSION));
    ui->fpgaVersion->setText(QString(tr(" fpga:  %1")).arg(QString::number(Fpga::getInstance()->readRegister(0xc), 16)));
    QFile file("/run/media/mmcblk1p1/sn.txt");
    file.open(QIODevice::ReadOnly);
    QDataStream s(&file);
    QString str;
    s >> str;
    ui->sn->setText("sn:"+str);
    file.close();
}

void EngineerUi::on_gohome_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void EngineerUi::on_goback_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(SystemSettingUi_Id);
}

void EngineerUi::functionBtnSlot(int id) {


    Mcu::getInstance()->touchSoundBeepMessage();
    ui->page->setCurrentIndex(id);
}

void EngineerUi::on_spiFlashSwitch_clicked(bool checked)
{
    uint8_t state = 0;

    Mcu::getInstance()->touchSoundBeepMessage();
    state = checked == true ? 0 : 1;
    Mcu::getInstance()->setSpiFlashSwitchMessage(state);
}

void EngineerUi::reportMcuVersionSlot(McuVersion *version)
{
    ui->mcuApp->setText(QString(tr(" mcu:  %1")).arg(version->mcuAppVersion));
    ui->mcuBoot->setText(QString(tr(" boot:  %1")).arg(version->mcuBootVersion));
}

void EngineerUi::on_screenshot_clicked(bool checked)
{
    int state = 0;

    Mcu::getInstance()->touchSoundBeepMessage();
    state = checked == true ? 0 : -1;
    RuntimeData::MiscData::getInstance()->g_miscDataInfo.screenshot = state;
}

void EngineerUi::on_temperature_clicked(bool checked) {
    RuntimeData::MiscData::getInstance()->enableTempratureMonitor = checked;
}

void EngineerUi::on_toolButton_clicked() {
   m_count++;
   if(m_count >= 5) {
        m_count = 0;
        ui->lineEdit_2->show();
        ui->pushButton->show();
   }
}

/**
 * @brief 用于进入对内工程模式
 */
void EngineerUi::on_pushButton_clicked() {
    if(ui->lineEdit_2->text() == "666666"){
        ui->onoff->show();
        ui->sensor->show();
        ui->lineEdit_2->clear();
    }
}

void EngineerUi::on_pushButton_2_clicked() {
    system("ifconfig eth0 down");
    system("ifconfig eth1 up");
    system("ifconfig eth1 192.168.1.31");
}

void EngineerUi::on_pushButton_3_clicked() {
    Mcu::getInstance()->OPenOrCloseAJL(true);
}

void EngineerUi::on_pushButton_4_clicked() {
    Mcu::getInstance()->OPenOrCloseAJL(false);
}

void EngineerUi::on_sensorRegister_6_clicked() {
    auto str = ui->lineEdit->text();
    QFile file("/run/media/mmcblk1p1/sn.txt");
    QDataStream s(&file);
    file.open(QIODevice::WriteOnly);
    s<<str;
    file.close();
}

void EngineerUi::on_pushButton_5_clicked() {
   setFpga[ImageCCM_EN](1, 0);
}

void EngineerUi::on_pushButton_6_clicked() {
   setFpga[ImageCCM_EN](0, 0);
}

/**
 * @brief EngineerUi::屏幕性能测试
 */
void EngineerUi::on_pushButton_7_clicked() {
    ui->gohome->hide();
    ui->goback->hide();
    ui->page->hide();
    ui->menu->hide();
    m_isStartScrennTest = 1;
    mousePressEvent(nullptr);
    m_backLabel->show();
}

void EngineerUi::mousePressEvent(QMouseEvent *event) {
    QString color;
    QString style("background-color:%1");

    if(m_isStartScrennTest > 5) {
        if(m_isStartScrennTest>=8) {
            m_isStartScrennTest = 0;
            m_backLabel->hide();
            ui->gohome->show();
            ui->goback->show();
            ui->page->show();
            ui->menu->show();
        }
        m_backLabel->setPixmap(QPixmap(QString(":/image/%1.png").arg(m_isStartScrennTest)).scaled(1280,800));
        m_isStartScrennTest++;
    }

    if(m_isStartScrennTest>0 && m_isStartScrennTest<6) {
        switch (m_isStartScrennTest) {
            case 1: color = "black" ;break;
            case 2: color = "white" ;break;
            case 3: color = "red" ;break;
            case 4: color = "green";break;
            case 5: color = "blue" ;break;
        }
        m_backLabel->setStyleSheet(style.arg(color));
        m_isStartScrennTest++;
    }
}


/**
 * @brief 打开寄存器读写页
 */
void EngineerUi::on_pushButton_8_clicked() {
    SensorInfo::getInstance().setIsOpenRegisterOperationPage(true);
}

/**
 * @brief 关闭寄存器读写页
 */
void EngineerUi::on_pushButton_10_clicked() {
    SensorInfo::getInstance().setIsOpenRegisterOperationPage(false);
}

void EngineerUi::on_pushButton_9_clicked() {
    RuntimeData::MiscData::getInstance()->setCrossLine(true);
}

void EngineerUi::on_pushButton_11_clicked() {
    RuntimeData::MiscData::getInstance()->setCrossLine(false);
}

